import { BASE_URL } from "../../utils/config";
interface IPageData {
  loginName: string;
  loginPwd: string;
}

Page<IPageData>({
  data: {
    loginName: '',
    loginPwd: ''
  },

  onLoad() {
    // 页面加载时的逻辑
  },

  handleLogin() {
    const { loginName, loginPwd } = this.data;
    if (!loginName || !loginPwd) {
      wx.showToast({
        title: '请输入账号和密码',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '登录中...'
    });

    wx.request({
      url: `${BASE_URL}/login`,
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      data: {
        loginName,
        loginPwd
      },
      success: (res: any) => {
        wx.hideLoading();
        const resp = res.data;
        if (resp.data && resp.data.token) {
          // 存储token
          wx.setStorageSync('token', resp.data.token);
          wx.showToast({
            title: '登录成功',
            icon: 'success'
          });
          
          // 登录成功后跳转到我的页面
          wx.switchTab({
            url: '/pages/my/my'
          });
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'error'
          });
        }
      },
      fail: (error) => {
        wx.hideLoading();
        console.error('登录请求失败', error);
        // wx.showToast({
        //   title: '网络错误',
        //   icon: 'error'
        // });
      }
    });
  }

})